﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace DAL
{
    public class Product
    {
        public static int insert(ProductEntity product)
        {
             return Convert.ToInt32(DbHelper.ExecuteScalar("p_Product_insert",
              product.ProductNo,
              product.Name,
              product.CategoryId,
              product.Unit,
              product.Standard,
              product.StandardInfor,
              product.Description,
              product.AutoAdd,
              product.IsSale,        
              product.Adminid,
              product.IsAutoPrice,
              product.IsCurrentCost
               ));
        }

        public static void update(ProductEntity product)
        {
            DbHelper.ExecuteNonQuery("p_Product_update",
              product.ProductId,
              product.ProductNo,
              product.Name,
              product.CategoryId,
              product.Unit,
              product.Standard,
              product.StandardInfor,
              product.Description,
              product.AutoAdd,
              product.IsSale,
              product.Adminid,
              product.IsAutoPrice,
              product.IsCurrentCost
               );
        }

        public static void delete(int productId)
        {
            DbHelper.ExecuteNonQuery("p_Product_delete",
                productId);
        }

        public static ProductEntity getById(int productId)
        {
            DataRow dr = DbHelper.ExecuteDataRow("p_product_getById", productId);
            ProductEntity product = new ProductEntity();
            if (dr != null)
            {
                product.ProductId = productId;
                product.ProductNo = dr["productNo"].ToString();
                product.Name = dr["name"].ToString();
                product.CategoryId = Convert.ToInt32(dr["categoryId"]);
                product.Unit = dr["unit"].ToString();
                product.Standard = dr["standard"].ToString();
                product.StandardInfor = dr["standardInfor"].ToString();
                product.Description = dr["description"].ToString();
                product.AutoAdd = Convert.ToDecimal(dr["autoAdd"]);
                product.IsSale = Convert.ToBoolean(dr["isSale"]);
                product.InputDt = Convert.ToDateTime(dr["inputDt"]);
                product.Adminid = Convert.ToInt32(dr["adminid"]);
                product.IsAutoPrice = Convert.ToBoolean(dr["isAutoPrice"]);
                product.ProductImage = dr["systemName"].ToString();
                product.IsCurrentCost = Convert.ToBoolean(dr["isCurrentCost"]);
            }
            return product;
        }

        public static DataTable getByCategory(int categoryId)
        {
            return DbHelper.ExecuteDataTable("p_Product_getByCategory", categoryId);
        }

        public static DataTable getByCategoryLimited(int categoryId,int companyId)
        {
            return DbHelper.ExecuteDataTable("p_Product_getByCategoryLimited", categoryId,companyId);
        }

        public static string getAutoProductNo(int categoryId)
        {
            return DbHelper.ExecuteScalar("p_Product_getAutoProductNo",
                categoryId).ToString();
        }


        public static bool isExistProductNo(int productId, string productNo)
        {

            if (Convert.ToInt32(DbHelper.ExecuteScalar("p_Product_isExistProductNo", productId, productNo)) == 1)
                return true;
            else
                return false;
        }

        
        public static DataTable listWithPriceByCompanyAndCategory(int companyId,int categoryId,string pros)
        {
            return DbHelper.ExecuteDataTable("p_Product_listWithPriceByCompanyAndCategory",companyId, categoryId,pros);
        }

        public static DataTable listWithPriceByCompanyAndProsuctes(int companyId,int pros,string nature)
        {
            return DbHelper.ExecuteDataTable("p_Product_listWithPriceByCompanyAndProducts", companyId, pros,nature);
        }
        

        public static DataTable listWithPriceByBuyer(int companyId)
        {
            return DbHelper.ExecuteDataTable("p_Product_listWithPriceByBuyer",companyId);
        }

        public static Decimal getBuyerPrice(int buyerId, decimal sellerPrice, int productId)
        {
            return Convert.ToDecimal(DbHelper.ExecuteScalar("p_Product_getBuyerPrice", buyerId,sellerPrice,productId));
        }
        public static void updateAutoAddByCategoryId(int categoryId, decimal autoAdd)
        {
            DbHelper.ExecuteNonQuery("p_Product_updateAutoAddByCategoryId", categoryId,autoAdd);
        }

        public static DataTable listWithPriceByCompanyAndRecepe(int companyId, string recepe)
        {
            return DbHelper.ExecuteDataTable("[p_Product_listWithPriceByCompanyAndRecepe]", companyId, recepe);
        }

        public static int CountProduct()
        {
            int tCount = 0;
            object tmp = DbHelper.ExecuteScalar("p_Product_CountAll");
            if (tmp!=null)
            {
                tCount = Convert.ToInt32(tmp);
            }
            return tCount;
        }

        public static void copyInsert(int productId, string rename, string fileName)
        {
            DbHelper.ExecuteNonQuery("p_product_Copy_insert", productId, rename, fileName);
        }

        public static DataTable getBuyRange(int productId)
        {
            return DbHelper.ExecuteDataTable("p_Product_getBuyRange", productId);
        }

        /// <summary>
        /// 根据产品编号和年份查询月平均毛利率
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="year"></param>
        /// <returns></returns>
        public static DataSet productMonthGPMlistByProductAndYear(int productId,int year)
        {
            return DbHelper.ExecuteReDataSet("p_ProductMonthGPM_listByProductAndYear", productId, year);
        }

        public static DataTable getSupplyCancelProductList(string productIds)
        {
            return DbHelper.ExecuteDataTable("p_Product_cancelSupply", productIds);
        }

        public static void productUpdateAutoAdd(int adminId, int productId, decimal newAutoPrice)
        {
            DbHelper.ExecuteNonQuery("p_Product_updateAutoAdd", productId, newAutoPrice, adminId);
        }

        public static DataTable listWithPriceByCompanyAndProsuctesSpecials(int companyId, int pros)
        {
           return DbHelper.ExecuteDataTable("p_Product_listWithPriceByCompanyAndProducts_Specials", companyId, pros);
        }

        public static DataTable listWithPriceByCompanyAndProsuctes(int companyId, int pros, string nature, int fpid)
        {
            return DbHelper.ExecuteDataTable("[dbo].[p_Product_listWithFixedPriceByCompanyAndProducts]", companyId, pros, nature, fpid);
        }
        public static DataRow getByProductNo(string ProductNo)
        {
            return DbHelper.ExecuteDataRow("[dbo].[p_product_getByproductNo]", ProductNo);
        }

        public static void productIsCurrentCancelProduct(int adminid, int productid, bool isCurrent)
        {
            DbHelper.ExecuteNonQuery("[dbo].[P_product_isCancelProductByIsCurrent]", productid, adminid, isCurrent);
        }
    }
}
